package com.riaya.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.riaya.entity.Squad;
import com.riaya.mapper.SquadMapper;
import com.riaya.service.SquadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class SquadServiceImpl implements SquadService {
    @Autowired
    private SquadMapper squadMapper;

    public PageInfo<Squad> getAllSquad(Integer pageNo, Integer pageSize, String name) {
        PageHelper.startPage(pageNo, pageSize);
        List<Squad> list = squadMapper.getAllSquads(name);
        PageInfo<Squad> pageInfo = new PageInfo<Squad>(list);
        return pageInfo;
    }



    /**
     * 根据id查询小组信息
     * @param id
     * @return querySquadId
     */

    public Squad querySquadId(int id) {
        return squadMapper.querySquadId(id);
    }
    /**
     * 添加小组信息
     * @param squad
     */

        @Transactional
        public int addSquad(Squad squad) {
            // 数据校验示例
            if (squad.getName() == null || squad.getName().trim().isEmpty()) {
                throw new IllegalArgumentException("队伍名称不能为空");
            }
            return squadMapper.insertSquad(squad);
        }

    /**
     * 修改小组信息
     * @param squad
     */

    public void updateSquad(Squad squad) {
        if (squad.getName() == null || squad.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("队伍名称不能为空");
        }
        squadMapper.updateSquad(squad);
        System.out.println("修改小组信息成功");
        System.out.println(squad);
        return squadMapper.updateSquad(squad);
    }

    /**
     * 删除小组信息
     * @param id
     */
    public boolean delSquad(int id) {
        return squadMapper.deleteSquad(id) > 0;
    }


}
